#include <dirent.h>
#include <errno.h>

#include "counterpartExceptions.h"

struct OpenDirectory
{
		OpenDirectory (const char * path) : fDirectory (opendir (path))
		{
			if (!fDirectory)
			{
				if (errno == ENOENT) return;
				Throw (Exceptions::Errno ( ));
			}
		}

		~OpenDirectory (void)
		{
			if (fDirectory)
			{
				if (closedir (fDirectory))
				{
					Throw (Exceptions::Errno ( ));
				}
			}
		}

		operator bool (void) { return !!fDirectory; }

		struct dirent * NextEntry (void)
		{
			errno = 0;

			struct dirent * dirEntry (readdir (fDirectory));
	
			if (!dirEntry && errno)
			{
				Throw (Exceptions::Errno ( ));
			}

			return dirEntry;
		}

	private :

		DIR * fDirectory;	
};
